package com.ruoyu.cloud.ai.chat.service;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.ruoyu.cloud.ai.chat.entity.data.BookingStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.NestedExceptionUtils;

import java.time.LocalDate;

@Slf4j
@Configuration
public class BookingTools {


    private final FlightBookingService flightBookingService;

    public BookingTools(FlightBookingService flightBookingService){
        this.flightBookingService=flightBookingService;
    }

    public record BookingDetailRequest(String bookingNumber, String name) {
    }

    public record ChangeBookingDatesRequest(String bookingNumber, String name, String date, String from, String to) {
    }

    public record CancelBookingRequest(String bookingNumber, String name) {
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public record BookingDetails(String bookingNumber, String name, LocalDate date, BookingStatus bookingStatus,
                                 String from, String to, String bookingClass) {
    }

    //    @Bean
//    @Description("获取机票预定详细信息")
   /* @Tool(name = "getBookingDetails", description = "获取机票预定详细信息")
    public Function<BookingDetailRequest, BookingDetails> getBookingDetails() {
        return request -> {
            try {
                System.out.println("================获取机票预定详细信息==============");
                return flightBookingService.getBookingDetails(request.bookingNumber(), request.name());
            } catch (Exception e) {
                log.warn("Booking details: {}", NestedExceptionUtils.getMostSpecificCause(e).getMessage());
                return new BookingDetails(request.bookingNumber(), request.name(), null, null, null, null, null);
            }
        };
    }

        @Bean
    @Description("修改机票预定日期")
    public Function<ChangeBookingDatesRequest, String> changeBooking() {
        return request -> {
            flightBookingService.changeBooking(request.bookingNumber(), request.name(), request.date(), request.from(),
                    request.to());
            System.out.println("=============修改机票预定日期==================");
            return "";
        };
    }
    @Bean
    @Description("取消机票预定")
    @Tool(description = "取消机票预定")
    public Function<CancelBookingRequest, String> cancelBooking() {
        return request -> {
            flightBookingService.cancelBooking(request.bookingNumber(), request.name());
            System.out.println("=============取消机票预定==================");
            return "";
        };
    }
    */


    @Tool(name = "getBookingDetails", description = "获取机票预定详细信息")
    public BookingDetails getBookingDetails(String bookingNumber, String name) {
        try {
            System.out.println("================获取机票预定详细信息==============");
            return flightBookingService.getBookingDetails(bookingNumber, name);
        } catch (Exception e) {
            log.warn("Booking details: {}", NestedExceptionUtils.getMostSpecificCause(e).getMessage());
            return new BookingDetails(bookingNumber, name, null, null, null, null, null);
        }
    }

    @Tool(description = "修改机票预定日期")
    public void changeBooking(String bookingNumber, String name, String newDate, String from, String to) {
        System.out.println("=============修改机票预定日期==================");
        flightBookingService.changeBooking(bookingNumber, name, newDate, from, to);
    }

    @Tool(name = "cancelBooking", description = "取消机票预定")
    public void cancelBooking(String bookingNumber, String name) {
        System.out.println("=============取消机票预定==================");
        flightBookingService.cancelBooking(bookingNumber, name);


    }


}
